var app =new Vue({
	el:"#app",
	data:{
		rows:"",
		pageSize:10,
		user:{},
		flag:"1",
		currentUsername:"",
		message:"",
		modalTitle:""
	},
	created:function(){
	},
	mounted:function(){
		//formValidator();
		this.getTableList();
		$(".fixed-table-toolbar .search > .form-control").attr("placeholder","按用户名或名称搜索");
	},
	methods:{
		getTableList:function(){
			$("#bootstrapTable").bootstrapTable({
				   method:"GET",
				   url:"./api/user/loadUser",
				   contentType:"application/json",
				   dataType:"json",
				   toolbar:"#toolbar",
				   search:true,
				   trimOnSearch:true,
				   searchOnEnterKey:true,
				   cache:false,
				   pagination:true,
				   pageList:[10,20],
				   pageSize:5,
				   pageNumber:1,
				   striped:true,
				   showRefresh:true,
				   showColumns:true,
				   sidePagination: "server",
				   checkboxHeader:false,
				  queryParams:function(params){
					  if(params.search=="按用户名或名称搜索"){
						  params.search = null;
					  }
					   return {
						   pageNo:(params.offset/params.limit)+1,
						   pageSize:params.limit,
						   searchKey:params.search
					   }
				   },
				  columns:[{
						field:'username',
						title:'用户名',
					},
					{
						field:'nickname',
						title:'昵称',
					},
					{
						title:'是管理员',
						field:'administrator',
						formatter:function(value,row,index){
							if(value){
								return '<input  type="checkbox" checked="checked"></input>';
							} else{
								return '<input  type="checkbox" ></input>';
							}
						}
					
					},
					{
						field:'enabled',
						title:'可用',
						formatter:function(value,row,index){
							if(value){
								return '<input  type="checkbox" checked="checked"></input>';
							} else{
								return '<input  type="checkbox"  ></input>';
							}
						}
					},{
						title:'操作',
						field:"username",
						align:"center",
						formatter:function(value,row,index){
							var rowStr=JSON.stringify(row);
							 var m ="<button class='tooltip-test  btn btn-success btn-sm '   title='修改'  onclick='modify("+ rowStr+")'>"
								 +"<span class='glyphicon  glyphicon-pencil'></span></button>";
							var d = '<button class="tooltip-test btn btn-warning btn-sm"  title="删除"  style="margin-left:10px;" onclick="deleteUser(\''
							 +row.username+'\')"><span class="glyphicon  glyphicon-remove"></span></button>';
							var s=  '<button class="tooltip-test btn btn-primary btn-sm"   title="重置密码" style="margin-left:10px;" onclick="reset(\''
								 +row.username+'\' )"><span class="glyphicon  glyphicon-wrench"></span></button>';
							return m+d+s;
						}
					}
					],
					onLoadSuccess:function(result){
						
					}
				})
		},
		checkForm:function(){
			
		},
		addUser:function(){
			$("#myModal").modal('show');
			this.user={};
			this.modalTitle="新增";
			this.flag="1";
		},
		submit:function(){
	/*		//开启校验
			$('form').data('bootstrapValidator').validate(function(){
			
				
			});*/
			/*if(!$('form').data('bootstrapValidator').isValid()){
				return 
			} */
        	var path = this.flag=="1" ? "./api/user/addUser" : "./api/user/modifyUser";
			 $.ajax({                
                 data: JSON.stringify(this.user),
                 type: this.flag=="1" ? "POST" : "PUT",
                 contentType : "application/json",
                 url: path,
                 success: function() {
                	 $("#myModal").modal('hide');
                	 $("#bootstrapTable").bootstrapTable('refresh');
                 }
             });
		},
		submitDelete:function(){
			$.ajax({
		        url: "./api/user/removeUser",
		        data: {
		            "username" : this.currentUsername
		        },
		        type: "GET",
		        success: function() {
		        	$("#confirmModal").modal("hide");
		        	$("#bootstrapTable").bootstrapTable('refresh');
		        }
		    });
		}
	}
	
})
function modify(row){
	$("#myModal").modal('show');
 	app.modalTitle="修改";
	app.flag="2";
	app.user=row;
}
function deleteUser(username){
	$("#confirmModal"). modal('show');
	app.message='是否要删除该用户';
	app.currentUsername=username;
}
function reset(username){
    $.ajax({
        data: {"username":username},
        type: "GET",
        contentType : "application/json",
        url: "./api/user/resetUserPassword",
        success: function() {
			app.$notify({
				title:"重置密码成功",
				type:'success',
				duration:3000,
				position:'bottom-right'
			})
        }
    });
}
/**
 * 弹出框隐藏时对校验器操作
 */
$("#myModal").on('hidden.bs.modal',function(e){
	//销毁校验器
//	$('form').data('bootstrapValidator').destroy();
	//重置检验器为空
//	$('form').data('bootstrapValidator',null)
	//添加校验器
//	formValidator();
});
/**
 * 添加一个校验器

function formValidator(){
	$('form').bootstrapValidator({
		message:'This value is not valid',
		feedbackIcons:{
			valid:'glyphicon glyphicon-ok',
			invalid:'glyphicon glyphicon-remove',
			validating:'glyphicon glyphicon-refresh'
		},
		fields:{
			username:{
				validators:{
					notEmpty:{
						message:'用户名不能为空'
					}
				}
			},
			password:{
				validators:{
				identical:{
						field:"confirmPassword",
						message:"两次密码不一致"
					},
					notEmpty:{
						message:'密码不能为空'
					}
				}
			},
			confirmPassword:{
				validators:{
					identical:{
						field:"password",
						message:"两次密码不一致"
					},
					notEmpty:{
						message:'密码不能为空'
					}
				}
			}
		}
	})
}  
 */



